package com.xiumu.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiumu.common.core.page.PageQuery;
import com.xiumu.common.core.result.ResultJSON;
import com.xiumu.pojo.sys.dto.DictDTO;
import com.xiumu.pojo.sys.dto.DictItemDTO;
import com.xiumu.pojo.sys.entity.DictEntity;
import com.xiumu.pojo.sys.entity.DictItem;
import com.xiumu.pojo.sys.query.DictQuery;
import com.xiumu.pojo.sys.vo.DictVO;
import com.xiumu.service.sys.DictItemService;
import com.xiumu.service.sys.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 字典
 *
 * @author XiuMu
 * @since 2025/1/1 11:30
 */
@RestController
public class DictController {

    @Autowired
    private DictService dictService;

    @Autowired
    private DictItemService dictItemService;

    /**
     * 分页查询
     * 可传递查询参数，按照某些字段分页查询
     *
     * @param pageQuery 分页条件，查询参数
     * @return
     */
    @PostMapping("/page/dict")
    public ResultJSON<Page<DictEntity>> page(@RequestBody PageQuery<DictQuery, DictEntity> pageQuery) {
        return ResultJSON.querySuccess(dictService.listPage(pageQuery));
    }

    /**
     * 查询全部, 包括所有的字典项
     */
    @GetMapping("/dict")
    public ResultJSON<List<DictVO>> list() {
        return ResultJSON.querySuccess(dictService.listALl());
    }

    /**
     * 通过 字典ID或者编码 查询字典下的字典值
     *
     * @param id       字典ID
     * @param dictCode 字典编码
     * @return
     */
    @GetMapping("/dict/item")
    public ResultJSON<List<DictItem>> find(@RequestParam(value = "id", required = false) Long id,
                                           @RequestParam(value = "dictCode", required = false) String dictCode) {
        return ResultJSON.querySuccess(dictItemService.listByDictIdOrDictCode(id, dictCode));
    }

    /**
     * 创建字典
     *
     * @param dictDTO dict 信息
     * @return
     */
    @PostMapping("/dict")
    public ResultJSON<Boolean> create(@Validated @RequestBody DictDTO dictDTO) {
        return ResultJSON.createSuccess(dictService.create(dictDTO));
    }

    /**
     * 更新字典
     *
     * @param id      字典ID
     * @param dictDTO dict 信息
     * @return
     */
    @PutMapping("/dict/{id}")
    public ResultJSON<Boolean> update(@PathVariable("id") Long id, @Validated @RequestBody DictDTO dictDTO) {
        return ResultJSON.modifySuccess(dictService.updateDict(id, dictDTO));
    }


    /**
     * 通过 ID 删除字典
     *
     * @param id 字典ID
     * @return
     */
    @DeleteMapping("/dict/{id}")
    public ResultJSON<Boolean> delete(@PathVariable("id") Long id) {
        return ResultJSON.deleteSuccess(dictService.deleteById(id));
    }

    /**
     * 创建字典项
     *
     * @param id 字典ID
     * @param itemDTO 字典项信息
     * @return
     */
    @PostMapping("/dict/{id}/item")
    public ResultJSON<Boolean> createItem(@PathVariable("id") Long id, @Validated @RequestBody DictItemDTO itemDTO) {
        return ResultJSON.createSuccess(dictService.createItem(id, itemDTO));
    }

    /**
     * 更新字典项
     *
     * @param id      字典项ID
     * @param dictItemDTO 字典项信息
     * @return
     */
    @PutMapping("/dict/item/{id}")
    public ResultJSON<Boolean> updateItem(@PathVariable("id") Long id, @Validated @RequestBody DictItemDTO dictItemDTO) {
        return ResultJSON.modifySuccess(dictService.updateDictItem(id, dictItemDTO));
    }

    /**
     * 通过 ID 删除字典项
     *
     * @param id 字典项ID
     * @return
     */
    @DeleteMapping("/dict/item/{id}")
    public ResultJSON<Boolean> deleteItem(@PathVariable("id") Long id) {
        return ResultJSON.deleteSuccess(dictItemService.removeById(id));
    }
}
